package com.baselib.android;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.baselib.android.utils.ListUtil;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseApp extends Application implements Application.ActivityLifecycleCallbacks {

    protected List<Activity> activityHistory = new ArrayList<>();


    @Override
    public void onCreate() {
        super.onCreate();
        this.instance = this;
        onAppCreate();
        activityHistory = new ArrayList<>();
        registerActivityLifecycleCallbacks(this);
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        activityHistory.add(activity);
    }

    public void clearCreateActivities() {
        if (!ListUtil.isNullOrEmpty(activityHistory)) {
            for (Activity activity : activityHistory) {
                if (activity != null && !activity.isFinishing()) {
                    if (!activity.isFinishing()) {
                        activity.finish();
                    }
                }
            }
        }
    }


    @Override
    public void onActivityStarted(Activity activity) {

    }


    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }


    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

    protected static BaseApp instance;

    public static BaseApp getInstance() {
        return instance;
    }

    public abstract void onAppCreate();


    String reqUserAgentFlag = "";

    public String getReqUserAgentFlag() {
        return reqUserAgentFlag;
    }

    public void setReqUserAgentFlag(String reqUserAgentFlag) {
        this.reqUserAgentFlag = reqUserAgentFlag;
    }
}
